package com.example.JPetStore.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.JPetStore.Common;
import com.example.JPetStore.domain.Address;
import com.example.JPetStore.domain.Cart;
import com.example.JPetStore.domain.User;
import com.example.JPetStore.dto.CartDto;
import com.example.JPetStore.mappers.AddressMapper;
import com.example.JPetStore.mappers.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Service
public class CartDao {

    @Autowired
    private CartMapper cartMapper;

    public ArrayList<CartDto> queryCarts(User user) {
        List<CartDto> cartList = cartMapper.queryCarts(user.getUserId());
        ArrayList<CartDto> cartArrayList = new ArrayList<>(cartList);
        return cartArrayList;
    }

    public CartDto queryCartById(String cartId, String userId) {
        CartDto cart = cartMapper.queryCartById(cartId,userId);
        return cart;
    }

    public void removeCart(String cateId, String userId) {
        cartMapper.delete((new QueryWrapper<Cart>()).eq("user_id", userId).eq("cart_id", cateId));
    }

    public void addToCart(String itemId, String userId) {
        Cart cart = new Cart();
        cart.setItemId(itemId);
        cart.setUserId(Integer.parseInt(userId));
        cartMapper.insert(cart);
    }
}
